package com.market.cash.utils.dialog

import android.content.Context
import android.graphics.Paint
import android.view.LayoutInflater
import android.view.View
import com.market.cash.R
import com.market.cash.databinding.CashCommentNormalDialogBinding
import com.market.cash.pro.ProDialog
import com.market.cash.utils.event.CashRootEventHelper
import com.market.cash.utils.event.CashRootEventType
import com.market.cash.utils.helper.CashRootCommentDialogHelper

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/28 16:26
 * @Annotation：评分 Normal
 */
class CashRootCommentNormalDialog: ProDialog {
    private lateinit var binding: CashCommentNormalDialogBinding
    private var mCallback: CashRootCommentDialogHelper.IStarDialogCallback? = null

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: CashRootCommentDialogHelper.IStarDialogCallback?) : super(context) {
        mCallback = callback
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: CashRootCommentDialogHelper.IStarDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = CashCommentNormalDialogBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
        binding.btnNotNow.paint.flags = Paint.UNDERLINE_TEXT_FLAG//下划线
        binding.btnNotNow.paint.isAntiAlias = true//抗锯齿
    }

    override fun initListener() {
        setOnClickListener(binding.imgClose) //关闭
        setOnClickListener(binding.btnNotNow)
        setOnClickListener(binding.btnRate)

        binding.ratingBar.setIsIndicator(false)
        binding.ratingBar.setOnRatingChangeListener { ratingBar, rating, fromUser ->
            CashRootEventHelper.addCashRootRequestEventAction(CashRootEventType.CLICK.CASHROOT_CLICK_USER_RATE_US_STAR,rating.toInt().toString())
        }
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        when(v!!.id){
            R.id.imgClose,
            R.id.btnNotNow -> {//关闭、取消
                CashRootEventHelper.addCashRootRequestEventAction(CashRootEventType.CLICK.CASHROOT_CLICK_USER_RATE_US_NOT_NOW,"")
                dismiss()
            }
            else -> {//确定
                val count = binding.ratingBar.rating.toInt()
                if (count <= 0){
                    return
                }
                if(mCallback != null){
                    mCallback!!.completeScoringCount(count)
                }
                dismiss()
            }
        }
    }

    fun setSibTextTips(tips: String){
        binding.textTips.text = tips
    }

    override fun onStart() {
        super.onStart()
        CashRootEventHelper.addCashRootRequestEventAction(CashRootEventType.DIALOG.CASHROOT_DIALOG_USER_RATE_SHOW,"Normal Dialog")
    }

    override fun dismiss() {
        super.dismiss()
        CashRootEventHelper.addCashRootRequestEventAction(CashRootEventType.DIALOG.CASHROOT_DIALOG_USER_RATE_DISMISS,"Normal Dialog")
    }
}